import config from "../config/config";
import Http from "../utils/Http";
import Crypt from "../utils/Crypt";
import constant from "../config/constant";

export default class ImageModel extends Http {

  constructor() {
    super();
    this.key = constant.image
  }

  getKey() {
    return this.key
  }

  // 获取远程图片
  getRemoteImage() {
    return new Promise(resolve => {
      let obj = wx.getStorageSync(this.key)
      if (obj) {
        resolve(Crypt.parse(obj))
      } else {
        this.request({
          url: '/h5/mini/image/get-main-image'
        }).then(response => {
          const { data } = response
          wx.setStorageSync(this.key, data)
          resolve(Crypt.parse(data))
        })
      }
    })
  }

  // news bg
  getNewsBg() {
    return new Promise(resolve => {
      this.request({
        url: '/h5/mini/image/get-news-bg'
      }).then(response => {
        const { data } = response
        resolve(Crypt.parse(data))
      })
    })
  }

  getWxImage() {
    return new Promise(resolve => {
      this.request({
        url: '/h5/mini/image/wx'
      }).then(response => {
        const { data } = response
        resolve(Crypt.parse(data))
      })
    })
  }

  // 下面都为新定制的首页
  homePage() {
    const key = 'new_home'
    return new Promise(resolve => {
      let obj = wx.getStorageSync(key)
      if (obj) {
        resolve(Crypt.parse(obj))
      } else {
        this.request({
          url: '/h5/mini/image/home'
        }).then(response => {
          const { data } = response
          // 关闭缓存
          // wx.setStorageSync(key, data)
          resolve(Crypt.parse(data))
        })
      }
    })
  }
}
